<?php

namespace app\admin\controller;

use app\admin\model\Admin;
use app\admin\model\gas\Gasoil;
use app\admin\model\goods_manage\Goods;
use app\common\controller\Backend;
use think\Session;
use app\admin\model\Oilpurchase as OilpurchaseModel;
use app\admin\model\Oilpurchasedetail as OilpurchasedetailModel;

/**
 * 
 *
 * @icon fa fa-circle-o
 */
class Oilpurchase extends Backend
{

    protected $agent_id = -1;
    protected $store_no = -1;
    protected $admin_id = -1;

    /**
     * Purchase模型对象
     * @var \app\admin\model\Purchase
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();

        $user_info = Session::get('admin');//获取当前登录用户信息
        $this->agent_id = $user_info['agent_id'];
        $this->store_no = $user_info['store_no'];
        $this->admin_id = $user_info['id'];

        $this->model = new \app\admin\model\Oilpurchase();
        $this->view->assign("statusList", $this->model->getStatusList());
    }

    /**
     * 查看
     *
     * @return string|Json
     * @throws \think\Exception
     * @throws DbException
     */
    public function index()
    {
        $status_type = $this->request->param('status_type');
        $p_id = $this->request->param('p_id');
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if (false === $this->request->isAjax()) {
           if($status_type){
               if($p_id){
                   //更新状态
                   OilpurchaseModel::where('id',$p_id)->update(['status'=>$status_type]);
               }else{
                   //生成新的进货单
                   $p_id = $this->createData($this->agent_id, $this->store_no,$this->admin_id,$status_type);
                   if($p_id){
                       //更新临时数据状态
                       OilpurchasedetailModel::where('oilpurchase_id',0)
                           ->where('agent_id', $this->agent_id)
                           ->where('store_no', $this->store_no)
                           ->where('admin_id', $this->admin_id)
                           ->update(['oilpurchase_id'=>$p_id]);
                   }
               }
               if($status_type == 2){
                   //查询该进货单全部商品
                   $all_pd = OilpurchasedetailModel::where('oilpurchase_id',$p_id)->select();
                   foreach ($all_pd as  $pd_item){
                       //入库后更新油品库存
                       Gasoil::where('gas_id',$pd_item->gasset_id)->setInc('oil_num',$pd_item->num);
                   }
               }

           }
            return $this->view->fetch();
        }
        //如果发送的来源是 Selectpage，则转发到 Selectpage
        if ($this->request->request('keyField')) {
            return $this->selectpage();
        }
        [$where, $sort, $order, $offset, $limit] = $this->buildparams();
        $list = $this->model
            ->where($where)
            ->where('agent_id', $this->agent_id)
            ->where('store_no', $this->store_no)
            ->order($sort, $order)
            ->paginate($limit);
        //获取全部admin信息
        $all_admin_user_tmp = Admin::all();
        $all_admin_user = [];
        foreach ($all_admin_user_tmp as $vale){
            $all_admin_user[$vale->id] = $vale;
        }
        $re_data_tmp = $list->items();
//        var_dump($all_admin_user);die;
        if($re_data_tmp){
            foreach ($re_data_tmp as $iii){
                $tmp = $iii;
                $tmp['create_admin_name'] = isset($all_admin_user[$iii->create_admin_id]->nickname) ? $all_admin_user[$iii->create_admin_id]->nickname : '';
                $re_data[] = $tmp;
                unset($tmp);
            }
        }
        $result = ['total' => $list->total(), 'rows' => $list->items()];
        return json($result);
    }

    public function createData($agent_id, $store_no,$create_admin_id,$status)
    {
        //先查询有没有临时数据
        $is_exist = OilpurchasedetailModel::where('oilpurchase_id',0)
            ->where('agent_id', $this->agent_id)
            ->where('store_no', $this->store_no)
            ->where('admin_id', $this->admin_id)
            ->select();
        if(!$is_exist){
            return false;
        }
        //生成进货单
        $insert_info['oilpurchase_no'] = 'oil_jin'.date('YmdHis').mt_rand(10000,99999);
        $insert_info['agent_id'] = $agent_id;
        $insert_info['store_no'] = $store_no;
        $insert_info['create_admin_id'] = $create_admin_id;
        $insert_info['status'] = $status;
        $PurchaseModel = new OilpurchaseModel();
        $PurchaseModel->save($insert_info);
        return $PurchaseModel->getLastInsID();
    }


}
